package com.youku.core.view.components
{
   import com.youku.core.view.interfaces.IVideo;
   import com.youku.utils.DropFrameTable;
   import com.youku.PlayerConfig;
   import com.youku.core.model.vo.VideoSegmentData;
   import com.youku.core.view.events.BaseVideoEvent;
   import flash.display.Sprite;
   import com.youku.utils.GetUrl;
   import com.youku.events.CoreEvent;
   import com.youku.interfaces.IVideoSegmentStatus;
   import com.youku.data.VideoSegmentStatus;
   import com.youku.interfaces.IPlayerProxy;
   
   public class OneNSCore extends BaseCore
   {
      
      public function OneNSCore(param1:IPlayerProxy, param2:CoreBase) {
         super(param1,param2);
         _dfTable = new DropFrameTable();
      }
      
      protected var _youkuvideo:IVideo;
      
      override public function init(param1:Number, param2:Number) : void {
         super.init(param1,param2);
         _dfTable = new DropFrameTable(1);
         this.createVideoList();
         _currentVideoNum = 0;
         _currentPlayVideo = this._youkuvideo;
      }
      
      override public function reInit() : void {
         super.reInit();
         this._youkuvideo.close();
         this._youkuvideo = null;
         _playListData = PlayerConfig.playListData;
         this.createVideoList();
         _currentVideoNum = 0;
         _dfTable.clear();
         _dfTable = null;
         _dfTable = new DropFrameTable(1);
      }
      
      override public function playWaitLoad(param1:Number = 0) : void {
         var _loc5_:VideoSegmentData = null;
         var _loc6_:String = null;
         var _loc2_:Array = [];
         var _loc3_:Array = _playListData.videoSegmentsDic[_playListData.fileType];
         var _loc4_:* = 0;
         while(_loc4_ < _loc3_.length)
         {
            _loc5_ = _loc3_[_loc4_];
            _loc6_ = this.getURL(_loc5_);
            _loc2_.push(_loc6_);
            _loc4_++;
         }
         (this._youkuvideo as YoukuStreamVideo).resetUrlList(_loc2_);
         super.playWaitLoad(param1);
      }
      
      override protected function getVideoByNum(param1:Number) : IVideo {
         return this._youkuvideo;
      }
      
      private function createVideoList() : void {
         var _loc7_:VideoSegmentData = null;
         var _loc8_:String = null;
         var _loc1_:Array = [];
         var _loc2_:Array = [];
         var _loc3_:Array = [];
         var _loc4_:Array = [];
         var _loc5_:Array = _playListData.videoSegmentsDic[_playListData.fileType];
         var _loc6_:* = 0;
         while(_loc6_ < _loc5_.length)
         {
            _loc7_ = _loc5_[_loc6_];
            _loc8_ = this.getURL(_loc7_);
            _loc1_.push(_loc8_);
            _loc2_.push(_loc7_.size);
            _loc3_.push(_loc7_.seconds);
            _loc4_.push(_loc7_.fileId);
            _loc6_++;
         }
         this._youkuvideo = new YoukuStreamVideo(_loc1_,_loc2_,_loc3_,_loc4_);
         this._youkuvideo.init(_loc5_[0],0);
         this._youkuvideo.addEventListener(BaseVideoEvent.PLAY_START,onVideoPlayStart);
         this._youkuvideo.addEventListener(BaseVideoEvent.PLAY_OVER,this.onVideoPlayOver);
         this._youkuvideo.addEventListener(BaseVideoEvent.JUMP_PLAY,this.onVideoJumpPlay);
         this._youkuvideo.addEventListener(BaseVideoEvent.CONNECT_START,onVideoConnectStart);
         this._youkuvideo.addEventListener(BaseVideoEvent.CONNECT_DONE,onVideoConnectDone);
         this._youkuvideo.addEventListener(BaseVideoEvent.LOAD_BUFFER_FULL,onVideoLoadBufferFull);
         this._youkuvideo.addEventListener(BaseVideoEvent.GOT_METADATA,onVideoGotMetadata);
         this._youkuvideo.addEventListener(BaseVideoEvent.PLAY_LOAD_ERROR,onVideoLoadError);
         this._youkuvideo.addEventListener(BaseVideoEvent.PLAY_MIDDLE_ERROR,this.onVideoMiddleError);
         this._youkuvideo.addEventListener(BaseVideoEvent.RTMP_FAIL_ERROR,onRTMPVideoFailError);
         this._youkuvideo.addEventListener(BaseVideoEvent.EARLY_REQUEST_ERROR,onVideoEarlyRequestError);
         this._youkuvideo.addEventListener(BaseVideoEvent.BUFFER_EMPTY,onVideoBufferEmpty);
         this._youkuvideo.addEventListener(BaseVideoEvent.BUFFER_FULL,onVideoBufferFull);
         this._youkuvideo.addEventListener(BaseVideoEvent.BUFFER_PERCENT,onVideoBufferPercent);
         this._youkuvideo.addEventListener(BaseVideoEvent.ONE_NS_CORE_ERROR,this.onOneNSCoreError);
         this._youkuvideo.addEventListener(BaseVideoEvent.ONE_NS_ONE_STREAM_END,this.onOneNSOneStreamEnd);
         this.addChild(this._youkuvideo as Sprite);
         this._youkuvideo.volume = _storeVolume;
         _videoArr.push(this._youkuvideo);
      }
      
      private function removeVideo() : void {
         this._youkuvideo.close();
         if(this.contains(this._youkuvideo as Sprite))
         {
            this.removeChild(this._youkuvideo as Sprite);
         }
         this._youkuvideo.removeEventListener(BaseVideoEvent.PLAY_START,onVideoPlayStart);
         this._youkuvideo.removeEventListener(BaseVideoEvent.PLAY_OVER,this.onVideoPlayOver);
         this._youkuvideo.removeEventListener(BaseVideoEvent.JUMP_PLAY,this.onVideoJumpPlay);
         this._youkuvideo.removeEventListener(BaseVideoEvent.CONNECT_START,onVideoConnectStart);
         this._youkuvideo.removeEventListener(BaseVideoEvent.CONNECT_DONE,onVideoConnectDone);
         this._youkuvideo.removeEventListener(BaseVideoEvent.LOAD_BUFFER_FULL,onVideoLoadBufferFull);
         this._youkuvideo.removeEventListener(BaseVideoEvent.GOT_METADATA,onVideoGotMetadata);
         this._youkuvideo.removeEventListener(BaseVideoEvent.PLAY_LOAD_ERROR,onVideoLoadError);
         this._youkuvideo.removeEventListener(BaseVideoEvent.PLAY_MIDDLE_ERROR,this.onVideoMiddleError);
         this._youkuvideo.removeEventListener(BaseVideoEvent.RTMP_FAIL_ERROR,onRTMPVideoFailError);
         this._youkuvideo.removeEventListener(BaseVideoEvent.BUFFER_EMPTY,onVideoBufferEmpty);
         this._youkuvideo.removeEventListener(BaseVideoEvent.BUFFER_FULL,onVideoBufferFull);
         this._youkuvideo.removeEventListener(BaseVideoEvent.BUFFER_PERCENT,onVideoBufferPercent);
         this._youkuvideo.removeEventListener(BaseVideoEvent.ONE_NS_CORE_ERROR,this.onOneNSCoreError);
         this._youkuvideo.removeEventListener(BaseVideoEvent.ONE_NS_ONE_STREAM_END,this.onOneNSOneStreamEnd);
         this._youkuvideo = null;
      }
      
      private function getURL(param1:VideoSegmentData) : String {
         var _loc2_:* = "http://k.youku.com/player/getFlvPath";
         _loc2_ = _loc2_ + GetUrl.getFileParameters(param1.no,_playListData,param1,false);
         return _loc2_;
      }
      
      override protected function onVideoPlayOver(param1:BaseVideoEvent) : void {
         var _loc2_:* = NaN;
         _loc2_ = param1.data.num == null?0:param1.data.num;
         _corebase.dispatchEvent(new CoreEvent(CoreEvent.SEND_DYNAMIC_REPORT,
            {
               "code":"3002",
               "number":_loc2_
            }));
         _currentLoadingNum = 0;
         _currentVideoNum = 0;
         _playOver = true;
         saveWatchingRecord(true);
         _corebase.dispatchEvent(new CoreEvent(CoreEvent.SEND_DYNAMIC_REPORT,
            {
               "code":"3008",
               "number":_loc2_
            }));
         _corebase.dispatchEvent(new CoreEvent(CoreEvent.PLAY_OVER));
      }
      
      override protected function onVideoMiddleError(param1:BaseVideoEvent) : void {
         this.onOneNSCoreError(null);
      }
      
      private function onOneNSCoreError(param1:BaseVideoEvent) : void {
         _corebase.tryToUseMultyCore(_nsTime);
      }
      
      private function onOneNSOneStreamEnd(param1:BaseVideoEvent) : void {
         var _loc2_:Number = param1.data.num == null?0:param1.data.num;
         _corebase.dispatchEvent(new CoreEvent(CoreEvent.SEND_DYNAMIC_REPORT,
            {
               "code":"3002",
               "number":_loc2_
            }));
      }
      
      override protected function onVideoJumpPlay(param1:BaseVideoEvent) : void {
         _currentVideoNum = 0;
         _currentLoadingNum = 0;
         _currentPlayVideo = this._youkuvideo;
         _corebase.dispatchEvent(new CoreEvent(CoreEvent.JUMP_PLAY));
      }
      
      override protected function updateTime() : void {
         var _loc1_:Number = 0;
         var _loc2_:Number = 0;
         _loc1_ = this._youkuvideo.loadedTime;
         _loc2_ = this._youkuvideo.nsTime;
         _loadedTime = _loc1_;
         _nsTime = _loc2_;
         var _loc3_:CoreEvent = new CoreEvent(CoreEvent.PLAYING,
            {
               "loadedTime":_loadedTime,
               "nsTime":_nsTime
            });
         _corebase.dispatchEvent(_loc3_);
         _loc3_ = null;
      }
      
      override protected function updateDroppedFrame() : void {
         _dfTable.update(0,this._youkuvideo.droppedFrames);
      }
      
      private function innerSeek(param1:Number, param2:Boolean, param3:Boolean = false) : void {
         _playOver = false;
         if(param1 < 0)
         {
            param1 = 0;
         }
         if(param1 > _playListData.totalTime)
         {
            param1 = _playListData.totalTime;
         }
         if(!_dragHold)
         {
            this._youkuvideo.resume();
            if(!param3)
            {
               _isPause = false;
            }
            _corebase.dispatchEvent(new CoreEvent(CoreEvent.RESUME));
         }
         if(param3)
         {
            this._youkuvideo.seekLoad(param1);
         }
         else
         {
            this._youkuvideo.seek(param1);
         }
         updateVideo();
      }
      
      override public function seek(param1:Number) : void {
         this.innerSeek(param1,true,false);
      }
      
      override public function mockSeek(param1:Number) : void {
         this.innerSeek(param1,false,false);
      }
      
      override public function seekLoad(param1:Number) : void {
         _isWaitingLoad = true;
         this.innerSeek(param1,true,true);
      }
      
      override public function play(param1:Number = 0) : void {
         this._youkuvideo.play();
         updateVideo();
      }
      
      override public function close() : void {
         super.close();
         this._youkuvideo.close();
      }
      
      override public function setQuality(param1:String) : void {
         var _loc2_:Number = _nsTime;
         _playListData.fileType = param1;
         _playListData.resetTimeArr();
         this.resetData();
         this.seek(_loc2_);
      }
      
      override public function resetData() : void {
         var _loc1_:Array = _playListData.videoSegmentsDic[_playListData.fileType];
         if(_loc1_ == null)
         {
            return;
         }
         _firstGetMetadata = true;
         this.removeVideo();
         this.createVideoList();
      }
      
      override public function getVideoSegmentStatus(param1:Number) : IVideoSegmentStatus {
         var _loc2_:IVideoSegmentStatus = new VideoSegmentStatus();
         _loc2_.speed = this._youkuvideo.speed;
         _loc2_.currSpeed = this._youkuvideo.currSpeed;
         _loc2_.startTime = this._youkuvideo.jumpT;
         _loc2_.loadedTime = this._youkuvideo.loadedTime;
         _loc2_.nsTime = this._youkuvideo.nsTime;
         _loc2_.fps = this._youkuvideo.FPS;
         return _loc2_;
      }
      
      override public function get currentPlayVideo() : IVideo {
         return this._youkuvideo;
      }
      
      override public function get currentVideoNum() : Number {
         return this._youkuvideo.num;
      }
      
      override public function set volume(param1:Number) : void {
         if(param1 < 0)
         {
            param1 = 0;
         }
         _storeVolume = param1;
         this._youkuvideo.volume = _storeVolume;
      }
   }
}
